<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麥當苗兒 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace User\Api;
define('UC_CLIENT_PATH', dirname(dirname(__FILE__)));

//載入配置文件
require_cache(UC_CLIENT_PATH . '/Conf/config.php');

//載入函數庫文件
require_cache(UC_CLIENT_PATH . '/Common/common.php');

/**
 * UC API調用控制器層
 * 調用方法 A('Uc/User', 'Api')->login($username, $password, $type);
 */
abstract class Api{

	/**
	 * API調用模型實例
	 * @access  protected
	 * @var object
	 */
	protected $model;

	/**
	 * 構造方法，檢測相關配置
	 */
	public function __construct(){
		//相關配置檢測
		defined('UC_APP_ID') || throw_exception('UC配置錯誤：缺少UC_APP_ID');
		defined('UC_API_TYPE') || throw_exception('UC配置錯誤：缺少UC_APP_API_TYPE');
		defined('UC_AUTH_KEY') || throw_exception('UC配置錯誤：缺少UC_APP_AUTH_KEY');
		defined('UC_DB_DSN') || throw_exception('UC配置錯誤：缺少UC_DB_DSN');
		defined('UC_TABLE_PREFIX') || throw_exception('UC配置錯誤：缺少UC_TABLE_PREFIX');
		if(UC_API_TYPE != 'Model' && UC_API_TYPE != 'Service'){
			throw_exception('UC配置錯誤：UC_API_TYPE只能為 Model 或 Service');
		}
		if(UC_API_TYPE == 'Service' && UC_AUTH_KEY == ''){
			throw_exception('UC配置錯誤：Service方式調用Api時UC_AUTH_KEY不能為空');
		}
		if(UC_API_TYPE == 'Model' && UC_DB_DSN == ''){
			throw_exception('UC配置錯誤：Model方式調用Api時UC_DB_DSN不能為空');
		}

		$this->_init();
	}

	/**
	 * 抽象方法，用於設置模型實例
	 */
	abstract protected function _init();

}